package com.ruoyi.coupon.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.coupon.domain.Coupon;
import com.ruoyi.coupon.service.ICouponService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券管理Controller
 *
 * @author ruoyi
 * @date 2024-09-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/coupon/coupon")
public class CouponController extends BaseController {

    private final ICouponService couponService;

/**
 * 查询优惠券管理列表
 */
@PreAuthorize("@ss.hasPermi('coupon:coupon:list')")
@GetMapping("/list")
    public TableDataInfo list(Coupon coupon) {
        startPage();
        LambdaQueryWrapper<Coupon> lqw = new LambdaQueryWrapper<Coupon>();
                        if (coupon.getCouponRequire() != null){
                        lqw.eq(Coupon::getCouponRequire ,coupon.getCouponRequire());
                    }
                        if (StringUtils.isNotBlank(coupon.getCouponName())){
                        lqw.like(Coupon::getCouponName ,coupon.getCouponName());
                    }
                        if (coupon.getStartTime() != null){
                        lqw.eq(Coupon::getStartTime ,coupon.getStartTime());
                    }
                        if (coupon.getEndTime() != null){
                        lqw.eq(Coupon::getEndTime ,coupon.getEndTime());
                    }
                        if (coupon.getCouponNum() != null){
                        lqw.eq(Coupon::getCouponNum ,coupon.getCouponNum());
                    }
                        if (coupon.getDiscount() != null){
                        lqw.eq(Coupon::getDiscount ,coupon.getDiscount());
                    }
                        if (StringUtils.isNotBlank(coupon.getStatus())){
                        lqw.eq(Coupon::getStatus ,coupon.getStatus());
                    }
        List<Coupon> list = couponService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.importTemplateExcel(response, "优惠券管理数据");
    }

    /**
     * 导入优惠券管理数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        List<Coupon> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            couponService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出优惠券管理列表
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:export')")
    @Log(title = "优惠券管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon) {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "优惠券管理数据");
    }

    /**
     * 获取优惠券管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId) {
        return AjaxResult.success(couponService.selectCouponByCouponId(couponId));
    }

    /**
     * 新增优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:add')")
    @Log(title = "优惠券管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupon coupon) {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:edit')")
    @Log(title = "优惠券管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupon coupon) {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:remove')")
    @Log(title = "优惠券管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds) {
        return toAjax(couponService.deleteCouponByCouponIds(couponIds));
    }
//    修改优惠券状态
    @PreAuthorize("@ss.hasPermi('coupon:coupon:edit')")
    @Log(title = "优惠券管理",businessType = BusinessType.UPDATE)
    @PutMapping("/status/{couponId}/{status}")
    public AjaxResult updateStatus(@PathVariable Long couponId,@PathVariable String status){
        Coupon coupon = couponService.selectCouponByCouponId(couponId);
        if (coupon == null) {
            return AjaxResult.error("优惠券不存在");
        }
        coupon.setStatus(status);  // 更新优惠券状态
        int result = couponService.updateCoupon(coupon);
        return toAjax(result);
    }

}


